﻿using SD.Infrastructure.Repository.EntityFrameworkCore;
using SD.Infrastructure.RepositoryBase;
using SlamDunk.MES.Domain.Entities.ConfigurationContext;
using SlamDunk.MES.Domain.IRepositories.Interfaces.ConfigurationContext;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SlamDunk.MES.Repository.Implements.ConfigurationContext
{
    /// <summary>
    /// 用户扩展仓储实现
    /// </summary>
    public class UserExtensionRepository : EFAggRootRepositoryProvider<UserExtension>, IUserExtensionRepository
    {
        #region # 分页获取用户扩展列表 —— ICollection<UserExtension> FindByPage(IList<Guid> facilityIds...
        /// <summary>
        /// 分页获取用户扩展列表
        /// </summary>
        /// <param name="facilityIds">工厂模型Id集</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="rowCount">总记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>用户扩展列表</returns>
        public ICollection<UserExtension> FindByPage(IList<Guid> facilityIds, int pageIndex, int pageSize, out int rowCount, out int pageCount)
        {
            IQueryable<UserExtension> userExtensions = base.FindAllInner();
            if (facilityIds != null && facilityIds.Any())
            {
                userExtensions = userExtensions.Where(x => facilityIds.Contains(x.FacilityId));
            }

            IOrderedQueryable<UserExtension> orderedResult = userExtensions.OrderByDescending(x => x.AddedTime);
            IQueryable<UserExtension> pagedResult = orderedResult.ToPage(pageIndex, pageSize, out rowCount, out pageCount);

            return pagedResult.ToList();
        }
        #endregion
    }
}
